package cn.kone.util;

import cn.dev33.satoken.stp.StpUtil;
import cn.kone.utils.AssertUtil;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;

import java.util.Map;

public class GtUtil {

    static final String appId = "AbJjbOYQbYAuRCMUgLMfa6";
    static final String appKey = "6QyVN8eOvB8cakUELcJlr5";
    static final String masterSecret = "nZEK5VVJAT6hQe55D5Yvg7";



    //推送单个消息
    public static void pushSingleMessage(String clientId,String message){
        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");

        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(appId);
        apiConfiguration.setAppKey(appKey);
        apiConfiguration.setMasterSecret(masterSecret);
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId,官网地址https://docs.getui.com/getui/server/rest_v2/standard/
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");

        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);//

        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        PushApi pushApi = apiHelper.creatApi(PushApi.class);//

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");

        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(message);//

        pushDTO.setPushMessage(pushMessage);

        /*设置接收人信息*/
        Audience audience = new Audience();

        pushDTO.setAudience(audience);//

        audience.addCid(clientId);//
        /*设置接收人信息结束*/

        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);//

        System.out.println("消息推送到APP端，推送结果是:"+apiResult);
        AssertUtil.isTrue(apiResult.isSuccess(),"消息推送失败:"+apiResult.getMsg());
    }

    public static void main(String[] args) {
//        GtUtil.pushSingleMessage("7efcb1d8095423033280be9684aa9823","我是李益,我给你发消息");
        GtUtil.pushSingleMessageByAlias("1775441497869516800","发给乘客的消息别名方式");
    }





    public static Object getuiBind(String cid) {
        String alias = StpUtil.getLoginIdAsString();//
        System.out.println(alias);

        //设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");

        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(appId);
        apiConfiguration.setAppKey(appKey);
        apiConfiguration.setMasterSecret(masterSecret);
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId,官网地址https://docs.getui.com/getui/server/rest_v2/standard/
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");

        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);//

        UserApi userApi = apiHelper.creatApi(UserApi.class);//

        CidAliasListDTO aliasDto = new CidAliasListDTO();
        aliasDto.add(new CidAliasListDTO.CidAlias(cid,alias));

        ApiResult<Void> apiResult = userApi.bindAlias(aliasDto);//
        System.out.println("个推绑定别名结果:"+apiResult);

        AssertUtil.isTrue(apiResult.isSuccess(),"绑定别名失败:"+apiResult.getMsg());
        return apiResult;
    }



    //推送单个消息
    public static void pushSingleMessageByAlias(String alias,String message){
        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");

        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(appId);
        apiConfiguration.setAppKey(appKey);
        apiConfiguration.setMasterSecret(masterSecret);
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId,官网地址https://docs.getui.com/getui/server/rest_v2/standard/
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");

        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);//

        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        PushApi pushApi = apiHelper.creatApi(PushApi.class);//

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");

        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(message);//

        pushDTO.setPushMessage(pushMessage);

        /*设置接收人信息*/
        Audience audience = new Audience();

        audience.addAlias(alias);//别名方式alias="1774619997721591808"
        pushDTO.setAudience(audience);//

        //audience.addCid(clientId);//
        /*设置接收人信息结束*/

        // 进行cid单推
        //ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);//
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByAlias(pushDTO);//别名方式

        System.out.println("消息推送到APP端，推送结果是:"+apiResult);
        AssertUtil.isTrue(apiResult.isSuccess(),"消息推送失败:"+apiResult.getMsg());
    }

}
